﻿#include "WidgetRoiList.h"
#include <QPushButton>
#include <QWidgetAction>
#include <QMenu>
#include <QMouseEvent>
#pragma execution_character_set("utf-8")
WidgetRoiList::WidgetRoiList(QWidget *parent)
    : QListView{parent}
{
    //添加选区列表的右键菜单
    mMenu = new QMenu(this);
    auto addAction = [&](QString text, void (WidgetRoiList::*func)())->QPushButton*{
        QPushButton* btn = new QPushButton(text);
        QWidgetAction* action = new QWidgetAction(this);
        action->setDefaultWidget(btn);
        connect(btn, &QPushButton::clicked, this, func);
        connect(btn, &QPushButton::clicked, mMenu, &QMenu::close);
        mMenu->addAction(action);
        return btn;
    };
    pbtnAdd = addAction("添加", &WidgetRoiList::sigRoiAdd);
    pbtnRemove = addAction("移除", &WidgetRoiList::sigRoiRemove);
    pbtnClear = addAction("清空", &WidgetRoiList::sigRoiClear);
}

void WidgetRoiList::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::RightButton){
        mMenu->exec(QCursor::pos());
    }
    QListView::mousePressEvent(e);
}
